package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Article;
import com.example.domain.entity.Product;
import com.example.domain.entity.ProductCategory;
import com.example.exception.FloraException;
import com.example.mapper.ProductCategoryMapper;
import com.example.service.ProductCategoryService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("productCategoryService")
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    //查询所有商品分类
    @Override
    public IPage<ProductCategory> getProductCategoryPage(String name, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductCategory> qw = new LambdaQueryWrapper<>();
        Page<ProductCategory> page = new Page<>(pageNum,pageSize);
        if(name!=null){
            qw.like(ProductCategory::getName,name);
        }

        return this.baseMapper.selectPage(page,qw);
    }

    //查询未禁用的商品分类
    @Override
    public List<ProductCategory> getProductCategoryList() {
        LambdaQueryWrapper<ProductCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(ProductCategory::getStatus,0);
        return this.baseMapper.selectList(qw);
    }

    //添加商品分类
    @Override
    @Transactional
    public ProductCategory add(ProductCategory productCategory) throws FloraException{
        try{
            this.baseMapper.insert(productCategory);
            return productCategory;
        } catch (Exception e) {
            throw new FloraException("添加商品分类失败");
        }
    }

    //修改分类
    @Override
    public void updateCommon(ProductCategory productCategory) throws FloraException {
        try{
            this.baseMapper.updateById(productCategory);
        } catch (Exception e) {
            throw new FloraException("修改商品分类失败");
        }
    }

    @Override
    public void updateStatus(ProductCategory productCategory) throws FloraException {
        Long categoryId = productCategory.getId();
        LambdaQueryWrapper<Product> qw = new LambdaQueryWrapper<>();

        if(productCategory.getStatus()==0){
            qw.eq(Product::getCategoryId,categoryId*(-1));
        }
        else{
            qw.eq(Product::getCategoryId, categoryId);
        }

        List<Product> productList = productService.list(qw);

        for(Product product:productList){
            Long id=product.getCategoryId();
            product.setCategoryId(id*(-1));
            productService.updateById(product);
        }

        try{
            this.baseMapper.updateById(productCategory);
        } catch (Exception e) {
            throw new FloraException("修改商品分类失败");
        }
    }

    @Override
    public void delete(Long id) throws FloraException {
        try{
            LambdaQueryWrapper<Product> qw = new LambdaQueryWrapper<>();
            qw.eq(Product::getCategoryId,id);
            List<Product> productList = productService.list(qw);

            for(Product product:productList){
                product.setCategoryId((long) 0);
                productService.updateById(product);
            }
            this.baseMapper.deleteById(id);
        } catch (Exception e) {
            throw new FloraException("删除商品分类失败"+e.getMessage());
        }
    }

}
